//
//  TZjingPInDetail.m
//  XMLYFM
//
//  Created by dllo on 15/12/25.
//  Copyright © 2015年 zzl. All rights reserved.
//

#import "TZjingPInDetail.h"
#import "AppTools.h"

//两种 cell
#import "TZJingPinSecion1.h"
#import "TZjingPinSection2Cell.h"
#import "SizeForLabel.h"

#import "TZjingPinDetailModel.h"


@interface TZjingPInDetail ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)NSMutableArray *allModelArray;
@property(nonatomic,strong)NSMutableDictionary *uerInfoDic;

@property(nonatomic,strong)UITableView *myTableView;

@property(nonatomic,assign)CGSize LabelSize;

@end

@implementation TZjingPInDetail

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"听单详情";
    self.view.backgroundColor = [UIColor yellowColor];
    //初始化字典和数组
    self.allModelArray = [NSMutableArray array];
    self.uerInfoDic = [NSMutableDictionary dictionary];
    
//    NSLog(@"3333f🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶%@",self.spicId);
    
    [self creatSubViews];
    [self getData];
    
    

}

-(void)creatSubViews
{
    self.myTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
//    self.myTableView.backgroundColor = [UIColor redColor];
    
    //注册第一种cell
    //自定义cell Xib注册
    [self.myTableView registerNib:[UINib nibWithNibName:@"TZJingPinSecion1" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TZJingPinSecion1"];
    
    //注册第二种cell
    [self.myTableView registerNib:[UINib nibWithNibName:@"TZjingPinSection2Cell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TZjingPinSection2Cell"];

    
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];



}



//数据请求
-(void)getData
{
    
    NSString *Str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/subject_detail?device=iphone&id=%@&position=1&title=精品听单",self.spicId];
//    NSLog(@"🐘🐘🐘🐘🐘%@",self.spicId);
    [AppTools getDataWithURL:Str Block:^(id result) {
        NSMutableDictionary *Dic = result;
        self.uerInfoDic = Dic[@"info"];
        NSMutableArray *list = Dic[@"list"];
        
        
        
        NSString *Str = self.uerInfoDic[@"intro"];
        
        CGSize size = [SizeForLabel labelRectWithSize:CGSizeMake(TZWIDTH - 24*2,MAXFLOAT) LabelText:Str Font:[UIFont systemFontOfSize:17]];
        self.LabelSize = size;
        
        
        
        for (NSMutableDictionary *dic in list) {
            TZjingPinDetailModel *detailModel = [[TZjingPinDetailModel alloc]init];
            [detailModel setValuesForKeysWithDictionary:dic];
            [self.allModelArray addObject:detailModel];
            
        }
        

        [self.myTableView reloadData];

        
        
    } ErrorBlock:^(NSError *error) {
        
        
    }];
    
    
    
    


}



//协议方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 2;


}

//头高
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }
    else
    {
    return 20;
    }

}

//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{
    if (indexPath.section == 0) {
        return self.LabelSize.height + 233;
    }
else
{
    return 200;
}

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    else
    {
    
    
        return self.allModelArray.count;
    }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    if (indexPath.section == 0) {
        static NSString *cellSection1ID = @"TZJingPinSecion1";
        TZJingPinSecion1 *cellsection1 = [tableView dequeueReusableCellWithIdentifier:cellSection1ID];
        cellsection1.dic = self.uerInfoDic;
        
        cellsection1.mySize = self.LabelSize;
        
        return cellsection1;
        
    }
    else
    {
        TZjingPinDetailModel *jingPin = self.allModelArray[indexPath.row];
        
    static NSString *cellId = @"TZjingPinSection2Cell";
    
    TZjingPinSection2Cell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        cell.JingPinDetail  = jingPin;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
    }

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    UIView* myView = [[UIView alloc] init] ;
    myView.backgroundColor = [UIColor colorWithRed:0.68 green:0.86 blue:0.89 alpha:1];
    return myView;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
